/* The Link = http://poj.org/problem?id=1059
 * I must give it a try
 * Accepted for just one submitt
 */

#include <iostream>
#include <vector> 

using namespace std;
const int MAX_THROWS = 1000;
// int firstPlayerThrows[MAX_THROWS], secondPlayerThrows[MAX_THROWS];
int totalThrows[MAX_THROWS];
int totals = 0;
int firsts = 0, seconds = 0;

// Assume there is no more than 100 chutes and ladders
typedef pair<int, int> int_pair;
// Chutes and ladders
vector<int_pair> chutes, ladders;
// lose-a-turns and extra-turns
vector<int> loses, extras;

bool isChutes(int square)
{
	vector<int_pair>::iterator iter = chutes.begin();
	while (iter != chutes.end()) {
		if (square == (*iter).first)
			return true;
		++iter;
	}
	return false;
}

int getChutes(int square)
{
	vector<int_pair>::iterator iter = chutes.begin();
	while (iter != chutes.end()) {
		if (square == (*iter).first)
			return (*iter).second;
		++iter;
	}
	return -1;
}

bool isLadders(int square)
{
	vector<int_pair>::iterator iter = ladders.begin();
	while (iter != ladders.end()) {
		if (square == (*iter).first)
			return true;
		++iter;
	}
	return false;
}

int getLadders(int square)
{
	vector<int_pair>::iterator iter = ladders.begin();
	while (iter != ladders.end()) {
		if (square == (*iter).first)
			return (*iter).second;
		++iter;
	}
	return -1;
}

bool isLoses(int square)
{
	vector<int>::iterator iter = loses.begin();
	while (iter != loses.end()) {
		if (square == (*iter))
			return true;
		++iter;
	}
	return false;
}

bool isExtras(int square)
{
	vector<int>::iterator iter = extras.begin();
	while (iter != extras.end()) {
		if (square == (*iter))
			return true;
		++iter;
	}
	return false;
}


int main(int argc, char *argv[])
{
	int throwNumber;
	int index = 1;
	
	// Read the throws of each player
	while (scanf("%d", &throwNumber) != EOF && throwNumber != 0) {
		//if (index == 1)
		//	firstPlayerThrows[firsts++] = throwNumber;
		//else 
		//	secondPlayerThrows[seconds++] = throwNumber;
		//++index;
		//if (index > 2)
		//	index %= 2;
		totalThrows[totals++] = throwNumber;
	}

	// Read each game set
	int numberOfPlayers;
	while (scanf("%d", &numberOfPlayers) != EOF && numberOfPlayers != 0) {
	
		// Read chutes and ladders
		chutes.clear();
		ladders.clear();
		int start, end;
		while (scanf("%d %d", &start, &end) != EOF && start != 0 && end != 0) {
			if (start < end) {
				// A ladder
				ladders.push_back(int_pair(start, end));
			} else {
				// A chute
				chutes.push_back(int_pair(start, end));
			}
		}

		// Read lose-a-turns and extra-turns, which may not exist
		loses.clear();
		extras.clear();
		int square;
		while (scanf("%d", &square) != EOF && square != 0) {
			if (square < 0)
				loses.push_back(-square);
			else
				extras.push_back(square);
		}

		// Begin to handle the major problem
		int *currentSquares = new int [numberOfPlayers];
		bool *playerLoses = new bool[numberOfPlayers];
		for (int i = 0; i < numberOfPlayers; i++) {
			currentSquares[i] = 0;
			playerLoses[i] = false;
		}

		// Games begins
		bool isReach = false;
		int winner = 0;
		// Current throw index
		int currentThrowIndex = 0;

		while (!isReach) {
			for (int i = 0; i < numberOfPlayers; i++) {
				if (playerLoses[i]) {
					playerLoses[i] = false;
				} else {
					//  A miss-a-turn or extra-turn square is never the start or end of a ladder or chute
					if (currentSquares[i] + totalThrows[currentThrowIndex] <= 100) {
						currentSquares[i] += totalThrows[currentThrowIndex];
						if (currentSquares[i] == 100) {
							isReach = true;
							winner = i;
							break;
						}
						// Handle the chutes and ladders
						int chute, ladder;
						while ((chute = getChutes(currentSquares[i])) != -1 || (ladder = getLadders(currentSquares[i])) != -1) {
							if (chute != -1) {
								currentSquares[i] = chute;
							} else {
								currentSquares[i] = ladder;
							}
						}

						// Handler the loses and extras
						if (isLoses(currentSquares[i]))
							playerLoses[i] = true;
						else if (isExtras(currentSquares[i])) {
							--i;
						}
					} 
					++currentThrowIndex;									
				}
			}
		}

		cout << winner + 1 << endl;

		delete[] currentSquares;

	}


	return 0;
}